home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 051-075 / scopedisk58 / kptr / ptr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-19  |  5.0 KB  |  205 lines

  1.  
  2. #include <exec/types.h>
  3. #include <exec/memory.h>
  4. #include <graphics/gfx.h>
  5. #include <graphics/sprite.h>
  6. #include <intuition/intuition.h>
  7.  
  8. #define SPMXWD      (16)
  9. #define SPMXHT      (16)
  10. #define SPRWD       (14)
  11. #define SPRHT       (16)
  12.  
  13. #define LREV        (1)     /* generic library revision     */
  14. #define INAM        ("intuition.library")
  15. #define GNAM        ("graphics.library")
  16.  
  17. typedef struct IntuitionBase *  ib_t;
  18. typedef struct GfxBase *        gb_t;
  19. typedef struct Preferences *    pr_t;
  20.  
  21. struct SpriteImage {            /* should be in graphics/sprite.h   */
  22.     UWORD posctl[2];
  23.     UWORD sprdata[2][SPRHT];
  24.     UWORD reserved[2];
  25. };
  26.  
  27. #define MEMFLAGS    (MEMF_CHIP|MEMF_PUBLIC|MEMF_CLEAR)
  28.  
  29. ib_t IntuitionBase;
  30. gb_t GfxBase;
  31.  
  32. struct Window *wp;
  33. struct Screen *sp;
  34. struct SpriteImage *si;
  35. struct Window *mywin;
  36.  
  37. APTR *OpenLibrary();
  38.  
  39. main(c, v)
  40. char **v;
  41. {
  42.     struct Preferences *prefbu;
  43.     register i;
  44.     int opxo, opyo;
  45.     int wd, ht;
  46.     USHORT *optr;
  47.     struct Window *getwin();
  48.     struct IntuiMessage *msg;
  49.     struct IntuiMessage *GetMsg();
  50.     ULONG Class;
  51.     void die(), modsi();
  52.  
  53.     if (*++v) {
  54.         if ((wd=atoi(*v))>SPMXWD) wd=SPMXWD;
  55.     }
  56.     else wd=SPRWD;
  57.     if (*++v){
  58.         if ((ht=atoi(*v))>SPMXHT) ht=SPMXHT;
  59.     }
  60.     else ht=SPRHT;
  61.     IntuitionBase=GfxBase=sp=si=mywin=NULL;
  62.  
  63.     if (!(IntuitionBase=(ib_t)OpenLibrary(INAM, LREV))) die();
  64.     if (!(GfxBase=(gb_t)OpenLibrary(GNAM, LREV))) die();
  65.     for (sp=IntuitionBase->FirstScreen; sp; sp=sp->NextScreen)
  66.         if ((sp->Flags&SCREENTYPE)==WBENCHSCREEN)
  67.             break;
  68.     if (!sp) die();     /* not likely   */
  69.     if (!(prefbu=(pr_t)AllocMem(sizeof(struct Preferences), MEMFLAGS)))
  70.         die();
  71.     GetPrefs(prefbu, sizeof(struct Preferences));
  72.     if (!(optr=AllocMem(sizeof(prefbu->PointerMatrix), MEMFLAGS))) die();
  73.     memcpy(optr, prefbu->PointerMatrix, sizeof(prefbu->PointerMatrix));
  74.     opxo=prefbu->XOffset;
  75.     opyo=prefbu->YOffset;
  76.     FreeMem(prefbu, sizeof(struct Preferences));
  77.     if (!(mywin=getwin(sp))) die();
  78.     if (!(si=getsi())) die();
  79.     for (;;){
  80.         if (msg=GetMsg(mywin->UserPort)){
  81.             Class=msg->Class;
  82.             ReplyMsg(msg);
  83.             if (Class==CLOSEWINDOW){
  84.                 while (msg=GetMsg(mywin->UserPort))
  85.                     ReplyMsg(msg);
  86.                 Forbid();
  87.                 for (wp=sp->FirstWindow; wp; wp=wp->NextWindow)
  88.                     SetPointer(wp, optr, SPMXHT, SPMXWD, opxo, opyo);
  89.                 Permit();
  90.                 die();
  91.             }
  92.         }
  93.         modsi(si, wd, ht);
  94.         wp=IntuitionBase->ActiveWindow;
  95.         WaitTOF();
  96.         SetPointer(wp, si, ht, wd, 0, 0);
  97.     }
  98.     /* NOTREACHED   */
  99. }
  100.  
  101. struct Window *
  102. getwin(sp)
  103. struct Screen *sp;
  104. {
  105.     struct NewWindow nw;
  106.     char *title="Pointer Abuzz!";
  107.  
  108.     memset(&nw, 0, sizeof(nw));
  109.     nw.Height=sp->BarHeight&0xFF;
  110.     nw.Width=TextLength(&sp->RastPort, title, strlen(title)&0xFFFF);
  111.     nw.Width+=84;  /* estimate of gadget size  */
  112.     nw.Title=title;
  113.     nw.LeftEdge=sp->Width/2-nw.Width/2;
  114.     nw.DetailPen=nw.BlockPen=-1;
  115.     nw.Flags=WINDOWDEPTH|WINDOWCLOSE|WINDOWDRAG|NOCAREREFRESH;
  116.     nw.IDCMPFlags=CLOSEWINDOW;
  117.     nw.Type=WBENCHSCREEN;
  118.     return(OpenWindow(&nw));
  119. }
  120.  
  121. struct SpriteImage *
  122. getsi(ht)
  123. {
  124.     struct SpriteImage *ts;
  125.  
  126.     if (!(ts=AllocMem(sizeof(*ts), MEMFLAGS))) return(NULL);
  127.     ts->posctl[1]=(ht<<8);   /* initialize sprite hite   */
  128.     return(ts);
  129. }
  130.  
  131. void
  132. modsi(si, wd, ht)
  133. struct SpriteImage *si;
  134. unsigned wd, ht;
  135. {
  136.     register i, color;
  137.     USHORT dot, msk, tmp1, tmp2, dw;
  138.     register USHORT *tsp;
  139.     static ypos, xpos, dx, dy;
  140.  
  141.     if (!dx){    /* initialize   */
  142.         dx=dy=1;
  143.         Forbid();
  144.         memset(si->sprdata, 0, sizeof(si->sprdata));
  145.         Permit();
  146.     }
  147.  
  148.     dw=SPMXWD-wd;   /* eliminate left margin when wd < max  */
  149.     i=ypos<<1;
  150.     msk=((1<<xpos)|(1<<((wd-1)-xpos))); /* get points of interest   */
  151.     color=Random(4);
  152.  
  153.     Forbid();
  154.     tsp=si->sprdata;    /* point to sprite's image          */
  155.  
  156.     dot=tsp[i]>>dw;     /* get current line's first word    */
  157.  
  158.     switch(color){
  159.         case 0:
  160.             tmp1=dot&~msk;  /* figure current line's new first word     */
  161.             tmp2=dot&~msk;  /* figure current line's new second word    */
  162.             break;
  163.         case 1:
  164.             tmp1=dot|msk;
  165.             tmp2=dot&~msk;
  166.             break;
  167.         case 2:
  168.             tmp1=dot&~msk;
  169.             tmp2=dot|msk;
  170.             break;
  171.         case 3:
  172.             tmp1=dot|msk;
  173.             tmp2=dot|msk;
  174.             break;
  175.     }
  176.     tmp1<<=dw;      /* left-justify the new line    */
  177.     tmp2<<=dw;
  178.     tsp[i]=tsp[((ht<<1)-2)-i]=tmp1;         /* set new 1st word */
  179.     tsp[i+1]=tsp[(((ht<<1)-2)-i)+1]=tmp2;   /* set new 2nd word */
  180.  
  181.     Permit();
  182.     ypos+=dy;
  183.     xpos+=dx;
  184.     if (ypos<0||ypos>=(ht>>1)){
  185.         dy*=-1;
  186.         ypos+=dy;
  187.     }
  188.     if (xpos<0||xpos>=(wd>>1)){
  189.         dx*=-1;
  190.         xpos+=dx;
  191.     }
  192.     return;
  193. }
  194.  
  195. void
  196. die()
  197. {
  198.     if (mywin) CloseWindow(mywin);
  199.     if (si) FreeMem(si, sizeof(*si));
  200.     if (GfxBase) CloseLibrary(GfxBase);
  201.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  202.     exit(700);
  203. }
  204.  
  205.